var app = getApp();
Component({
    properties: {
        list: Array,
        params : Object,
        color : String,
    },
    options : {
        addGlobalClass : true,
    },
    data:{
        showItemMenuIndex : -1,
        isshowrpt : false,
        reportid:0,
        istop : 0,
        times : 0,
        tarid : '',
        playindex : -1,
    },
    methods: {
        setDataTap(e){
            this.setData({
                showItemMenuIndex : e.currentTarget.dataset.i
            })
        },
        navigateto : function( e ){
            app.com.navigateto( e );
        },
        location(e){
            app.com.location( e );
        },
        callphone : function(e){
            app.com.callphone( e );
        },
        showfn(){
            this.setData({
                isshow : !this.data.isshow
            })
        },
        likeit(e){
            var self = this;
            var index = e.currentTarget.dataset.i;
            if( self.data.list[index].islike*1 == 1 ) return false;
            var postdata = {
                op : 'likeit',
                id : self.data.list[index].id,
            };

            self.post(postdata,function(){
                self.setData({
                    ['list['+index+'].like'] : self.data.list[index].like*1 + 1,
                    ['list['+index+'].islike'] : 1,
                })
            });
        },
        toReport(e){
            var self = this;
            var id = e.currentTarget.dataset.id;
            self.setData({
                reportid : id,
                reportcontent : '',
                isshowrpt : true,
            })
        },
        onPreviewTap(e){
            var self = this;
            var index = e.currentTarget.dataset.index;
            var inn = e.currentTarget.dataset.inn;
            wx.previewImage({
                urls : self.data.list[index].img,
                current : self.data.list[index].img[inn],
            })
        },
        subreport(){
            var self = this;
            var postdata = {
                op : 'subreport',
                cid : self.data.reportid,
                content : self.data.reportcontent,
            };

            self.post(postdata,function(res){
                self.setData({
                    isshowrpt : false,
                })
                app.com.toast(res.data.message,'none');
            });
        },
        reportcontentfn(e){
            this.setData({
                reportcontent : e.detail.value,
            })
        },
        delit(e){
            var self = this;
            var index = e.currentTarget.dataset.i;
            var postdata = {
                op : 'delit',
                id : self.data.list[index].id,
            };
            app.com.confirm('确定要删除此条数据吗？',function(){
                self.post(postdata,function(){
                    self.data.list.splice(index,1);
                    self.setData({
                        list : self.data.list,
                        showItemMenuIndex : -1,
                    })
                    app.com.toast('已删除','none');
                });
            })
        },
        post(postdata,fn){
            app.com.http('cityinfo','POST',postdata,0,true,'','',function(res){
                if (!res.data.errno && res.data) {
                    if(fn) fn(res);
                }else{
                    app.com.alert(res.data.message);
                }
            })
        },
        paytel(e){
            this.data.istop = 0;
            var self = this;
            self.data.times = 0;
            var i = e.currentTarget.dataset.i;
            var postdata = {
                op : 'paytel',
                cid : e.currentTarget.dataset.id
            };
            app.com.http('cityinfo','POST',postdata,0,true,'','',function(res){
                if (!res.data.errno && res.data) {
                    
                    wx.requestPayment({
                        appId : res.data.data.appId,
                        timeStamp: res.data.data.timeStamp,
                        nonceStr: res.data.data.nonceStr,
                        package: res.data.data.package,
                        signType: 'MD5',
                        paySign: res.data.data.paySign,
                        success: function (payres) {
                            if( payres.errMsg == 'requestPayment:ok' ){
                                self.seetel(postdata.cid,i);
                            }
                        },
                        fail: function (payres) {}
                    });
                }else{
                    app.com.alert(res.data.message);
                }
            });
        },
        seetel(cid,index){
            if(this.data.istop == 1) return false;
            var self = this;
            self.data.times ++;

            app.com.http('cityinfo','POST',{cid:cid,op:'seetel'},0,true,'','',function(res){
                if (!res.data.errno && res.data) {
                    self.setData({
                        ['list['+index+'].tel'] : res.data.data.tel,
                        ['list['+index+'].iscantel'] : 1,
                        times : 0,
                    })
                    app.com.toast('已支付成功，可点击按钮拨打电话','none');
                }else{
                    setTimeout(function(){
                        if(self.data.times <= 20) self.seetel(cid,index);
                    },1000)
                }
            })
        },
        stoppay(){
            this.data.istop = 1;
        },
        shareit(e){
            var self = this;
            this.setData({
                tarid : e.currentTarget.dataset.id,
            },function(){
                self.selectComponent('#poster').onClose();
            })
        },
        payvedio(e){
            var index = e.currentTarget.dataset.i;
            this.setData({
                playindex : index,
            })
        }
    }

})